package org.zachary.creditbusiness.commodityandhouse.controller;

import org.zachary.creditbusiness.commodityandhouse.entity.HouseOrder;
import org.zachary.creditbusiness.commodityandhouse.service.HouseOrderService;

import org.zachary.creditbusiness.commodityandhouse.scheduler.HouseOrderComplianceScheduler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.Map;


@RestController
@RequestMapping("/houseOrders")
public class HouseOrderController {

    @Autowired
    private HouseOrderService houseOrderService;


    @Autowired
    private HouseOrderComplianceScheduler complianceScheduler;


    @PostMapping("/placeOrder")
    public String placeHouseOrder(@RequestBody HouseOrder houseOrder) {
        houseOrderService.placeHouseOrder(houseOrder);
        return "Order placed successfully";
    }








    @PostMapping
    public int insert(@RequestBody HouseOrder houseOrder) {
        return houseOrderService.insert(houseOrder);
    }

    @PutMapping
    public int update(@RequestBody HouseOrder houseOrder) {
        return houseOrderService.update(houseOrder);
    }


    @PutMapping("/updateStatus")
    public int updateStatus(@RequestBody Map<String, Object> updateData) {
        Integer orderId = (Integer) updateData.get("id");
        Integer status = (Integer) updateData.get("status");
        String payDate = (String) updateData.get("payDate");

        return houseOrderService.updateOrderStatus(orderId, status, payDate);
    }


    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Integer id) {
        return houseOrderService.deleteById(id);
    }

    @GetMapping("/{id}")
    public HouseOrder selectById(@PathVariable Integer id) {
        return houseOrderService.selectById(id);
    }

    @GetMapping
    public List<HouseOrder> selectAll() {
        return houseOrderService.selectAll();
    }

    @GetMapping("/user/{userId}")
    public List<HouseOrder> getOrdersByUserId(@PathVariable Integer userId) {
        return houseOrderService.getOrdersByUserId(userId);
    }


    @PostMapping("/pay/{orderId}")
    public String payHouseOrder(@PathVariable Integer orderId) {
        try {
            houseOrderService.payHouseOrder(orderId);
            return "Payment successful";
        } catch (Exception e) {
            return "Payment failed: " + e.getMessage();
        }
    }

    @PostMapping("/updateCompliance")
    public String updateComplianceStatus() {
        try {
            complianceScheduler.updateHouseOrderComplianceStatus();
            return "Compliance status updated successfully";
        } catch (Exception e) {
            return "Update failed: " + e.getMessage();
        }
    }

}








